package edu.chds.ui
{
	import com.allthingsmotion.utils.MathXML;
	
	import edu.chds.data.xml.TimelineReader;
	import edu.chds.utils.TimelineLoader;
	
	import fl.controls.Button;
	import fl.controls.ComboBox;
	import fl.controls.TextArea;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.easing.*;
	import gs.*;

	public class Timeline extends MovieClip
	{
		public static const COMPLETE_NODES:String = "completeNodes";
		public static const COMPLETE_LINE:String = "completeLine";
		private var _yearBuffer:Number = 15; //amount of years that the timeline extends left or right from _minYear && _maxYear		
		
		public var xmlPath:String;
		public var cbType:ComboBox;
		public var cbYear:ComboBox;
		public var taKeywords:TextArea;
		public var btSearch:Button;
		public var btSearchKey:Button;
		public var taList:TextArea;
		public var timelineLoader:TimelineLoader;
		
		public var reader:TimelineReader;
		public var lines:Array = new Array();
		public var linesContainer:MovieClip = new MovieClip();
		public var nodes:Array = new Array;		
		public var nodesContainer:MovieClip = new MovieClip();
		private var _minYear:Number;
		private var _maxYear:Number;
		private var _minYearBuffer:Number;
		private var _maxYearBuffer:Number;
		private var _nodesAverage:Number;
		
				
		public function Timeline()
		{
			/* xmlPath = path;
			cbType = cb;
			cbYear = cbY;
			taKeywords = taK;
			btSearch = btS;
			btSearchKey = btK;
			taList = ta;
			
			timelineLoader = new TimelineLoader(path, this);
			*/
			this.addChild(linesContainer);
			this.addChild(nodesContainer);							
			init();
		}
		private function init():void {
			linesContainer.x = nodesContainer.x = 0;
			linesContainer.y = nodesContainer.y = 0;
		}
		public function generateTimeline(tReader:TimelineReader):void {
			trace("Generate Timeline");
			reader = tReader;
			var yearX:Number = 100;
			var yearY:Number = stage.stageHeight;
			
			for (var i:int = _minYearBuffer; i <= _maxYearBuffer; i++){
				var timelineYear:TimelineYear = new TimelineYear();
				linesContainer.addChild(timelineYear);
				lines.push(timelineYear);
				
				//else if statements to size the years in the timeline according to printed version
				if (i < 2001) {
					//trace ("Normal size");
				} else if (i >= 2001 && i < 2004) {
					timelineYear.bg.width *= 10;
				} else if (i >= 2004 && i <= _maxYear) {
					timelineYear.bg.width *= 15;
				} else if (i > _maxYear) {
					
				} else {
					trace ("ERROR in reading data");
				}
				timelineYear.x = yearX;
				timelineYear.y = yearY - timelineYear.height;
				timelineYear.year.text = i.toString();	
							
				//YearX += needs to be before -= timelineYear.year.width/2
				//because the year textfield will be pushed to the left and increase width)
				yearX += timelineYear.width;
				timelineYear.year.x -= timelineYear.year.width/2;	
//				trace("timelineYear.x " + timelineYear.x);
//				trace("timelineYear.y " + timelineYear.y);
			}
			dispatchEvent(new Event(Timeline.COMPLETE_LINE));
		}
		public function generateNodes(tReader:TimelineReader):void {
			trace("Generate Nodes");
			reader = tReader;  
			for each(var item:XML in reader.documents) {
//				trace (item.title);
				var timelineNode:TimelineNode = new TimelineNode();
				nodesContainer.addChild(timelineNode);
				nodes.push(timelineNode);
				trace("timelineNOde.active " + timelineNode.active);
//				trace("item.index " + reader.documents);
				timelineNode.year = item.year;
				timelineNode.month = MathXML.monthToValue(String(item.month));
//				timelineNode.xpos = _minYear;
											
				timelineNode.title.text = item.title;
				timelineNode.type.text = item.type;
				timelineNode.date.text = item.month + " " + item.year;
				
//				timelineNode.x = timelineNode.xpos * (timelineNode.width + 5);
				timelineNode.y = 100;//Math.random() * stage.stageHeight/2 + timelineNode.height/2;//Math.random() * stage.stageHeight;
				
				//Cycle through lines array to find where both years are == 
				//then place timelineNode.x = timelineYear.x
				for(var j:int = 0; j < lines.length - 1; j++) {
					var yearToNumber:Number = Number(lines[j].year.text);
					if (timelineNode.year == yearToNumber) {
						timelineNode.x = lines[j].x + timelineNode.width/2;
					} 
				}
				timelineNode.buttonMode = true;
				timelineNode.addEventListener(MouseEvent.CLICK, nodeClick);
			}
			//Dispatch Event for when after TimelineNodes are generated and on stage
			dispatchEvent(new Event(Timeline.COMPLETE_NODES));
		}
		/**
		 *
		 * 
		 * 
		 * LEFT OFF 090415
		 * Something wrong with node.height gives incorrect value
		 * also 0 value for node.y is on 100?
		 * 
		 * 
		 *  
		 * @param e
		 * 
		 */		
		private function nodeClick(e:MouseEvent):void {
			var node:TimelineNode = TimelineNode(e.currentTarget);
//			var activeNode:TimelineNode;
			
			trace('BEFOREnode.y ' + node.y);
			trace('BEFOREnode.height ' + node.height);
			
			for (var i:int = 0; i < nodes.length; i++) {
			
				if (node == nodes[i]) {
					node.removeEventListener(MouseEvent.CLICK, nodeClick);
//					node.active = true;
					TweenMax.to(node, 0.75, {scaleY:1.5, scaleX:1.5, ease:Back.easeIn, onComplete:checkOffStage, onCompleteParams:[node]});
				} else {
//					node.active = false;
 					TweenMax.to(nodes[i], 0.75, {scaleY:1, scaleX:1, ease:Back.easeIn, onComplete:function():void{
						node.addEventListener(MouseEvent.CLICK, nodeClick);
//						node.active = false;
					}});	 
				} 
			}
			trace ("CLICK");
		}
		private function checkOffStage(nodeClicked:TimelineNode):void {
			trace("checkOffStage");
			var node:TimelineNode = nodeClicked;
			var nodeLeft:Number = nodesContainer.x + (node.x - node.width/2);
			var nodeTop:Number = nodesContainer.y + (node.y - 118);
			var nodeRight:Number = nodeLeft + node.width;
			var nodeBottom:Number = nodeTop + node.height;
			
			var left:Number = 0;
			var top:Number = 0;
			var right:Number = stage.stageWidth;
			var bottom:Number = stage.stageHeight;

			trace('BEFOREnode.y ' + node.y);
			trace('BEFOREnode.height ' + node.height);
						
			trace('nodeTop ' + nodeTop);
			
			if ( node.y - 118 < top) {
				node.y = top + 118; 
			}
			nodeTop = nodesContainer.y + (node.y - node.height);
			trace('nodeTop ' + nodeTop);
			trace('node.y ' + node.y);
		}
		
		/**
		 * Get and Set methods 
		 * @return 
		 * 
		 */
		 		
		public function get minYear():Number {
			return _minYear;
		}
		public function set minYear(yearMin:Number):void {
			_minYear = yearMin;
			_minYearBuffer = _minYear - _yearBuffer;
		}
		public function get maxYear():Number {
			return _maxYear;
		}
		public function set maxYear(yearMax:Number):void {
			_maxYear = yearMax;
			_maxYearBuffer = _maxYear + _yearBuffer;
		}
		public function get nodesAverage():Number {
			return _nodesAverage;
		}
		public function set nodesAverage(yearAv:Number):void {
			_nodesAverage = yearAv;
		}
	}
}